const INIT_STATE = {
  userId: JSON.parse(sessionStorage.getItem("data"))?.userId || null,
  username: JSON.parse(sessionStorage.getItem("data"))?.username || null,
  phone: JSON.parse(sessionStorage.getItem("data"))?.phone || null,
  authList: JSON.parse(sessionStorage.getItem("data"))?.authList || [],
  token: JSON.parse(sessionStorage.getItem("data"))?.token || null,
};

export default function userReducer(state = INIT_STATE, action) {
  switch (action.type) {
    case "LOGIN":
      return {
        ...state,
        ...action.payload,
      };

    case "LOGOUT":
      return {
        userId: null,
        username: null,
        authList: [],
        token: null,
      };

    default:
      return state;
  }
}
